home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / GeometryTest / GTShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-14  |  12.4 KB  |  519 lines  |  [TEXT/MPCC]

  1. // Metafile read - shows how to read metafile data.
  2. //
  3. // Nick Thompson - January 6th 1994
  4. //
  5. // © 1995, Apple Computer Inc., All Rights Reserved
  6.  
  7. // system headers
  8. #include <Devices.h>
  9. #include <Dialogs.h>
  10. #include <DiskInit.h>
  11. #include <Fonts.h>
  12. #include <Menus.h>
  13. #include <PictUtils.h>
  14. #include <QDOffScreen.h>
  15. #include <QuickDraw.h>
  16. #include <SegLoad.h>
  17. #include <StandardFile.h>
  18. #include <TextEdit.h>
  19. #include <ToolUtils.h>
  20.  
  21. // for QuickDraw 3D
  22. #include "QD3D.h"
  23. #include "QD3DMath.h"
  24. #include "QD3DDrawContext.h"
  25. #include "QD3DShader.h"
  26. #include "QD3DTransform.h"
  27. #include "QD3DGroup.h"
  28. #include "QD3DCamera.h"
  29.  
  30. #include "GTFile.h"
  31. #include "GTShell.h"
  32. #include "GTSupport.h"
  33.  
  34. #include "Geometries.h"
  35. #include "MyErrorHandler.h"
  36.  
  37. //-------------------------------------------------------------------------------------------
  38. // function prototypes
  39.  
  40. static void         InitToolbox( void ) ;
  41. static void         MainEventLoop( void ) ;
  42. static void         HandleKeyPress(EventRecord *event) ;
  43. static void         HandleOSEvent(EventRecord *event) ;
  44. static TQ3Status     DocumentDraw3DData( DocumentHdl theDocument ) ;
  45.  
  46. //-------------------------------------------------------------------------------------------
  47. //
  48.  
  49. Boolean         gQuitFlag         = false ;
  50. CursHandle        gWatchCursor ;
  51.  
  52. //-------------------------------------------------------------------------------------------
  53. // main()
  54. // entry point for the application, initialize the toolbox, initialize QuickDraw 3D
  55. // and enter the main event loop.  On exit from the main event loop, we want to call
  56. // the QuickDraw 3D exit function to clean up QuickDraw 3d.
  57.  
  58. void main(void)
  59. {
  60.     TQ3Status    myStatus;
  61.     Rect        rBounds = { 50, 50, 350, 350 } ;
  62.     Str255        title = "\pSpinning Box" ;
  63.     FSSpec        theFileSpec ;                // the file we are opening
  64.  
  65.     InitToolbox() ;
  66.     
  67.     SetCursor(*(GetCursor(watchCursor)));
  68.     
  69.     //    Initialize QuickDraw 3D, open a connection to the QuickDraw 3D library
  70.     myStatus = Q3Initialize();
  71.     if ( myStatus == kQ3Failure ) {
  72.         DebugStr("\pErInitialize returned failure.");    
  73.         ExitToShell() ;
  74.     }
  75.                     
  76.     // install the error handler - this gets called whenever
  77.     // an error occurs, which means we don't have to check so 
  78.     // much
  79.     Q3Error_Register( MyErrorHandler, 0L );        
  80.     Q3Warning_Register( MyWarningHandler, 0L );        
  81.  
  82.     // set up our globals
  83.     gQuitFlag = false ;
  84.     
  85.     // loop till we doop
  86.     MainEventLoop();
  87.     
  88.     //    Close our connection to the QuickDraw 3D library
  89.     myStatus = Q3Exit();
  90.     if ( myStatus == kQ3Failure )
  91.         DebugStr("\pErExit returned failure.");
  92. }    
  93.  
  94.  
  95. //-----------------------------------------------------------------------------
  96. // assumes the port is set up before being called
  97.  
  98. TQ3Status DocumentDraw3DData( DocumentHdl theDocument )
  99. {
  100.     TQ3Vector3D                globalScale;
  101.     TQ3Vector3D                globalTranslate;
  102. //{
  103. //    TQ3CameraData        theCameraData ;
  104. //    TQ3CameraObject        theCameraObject;
  105. //    
  106. //    Q3View_GetCamera(theDocument->fView, &theCameraObject);
  107. //    Q3Camera_GetData(theCameraObject, &theCameraData);
  108. //Debugger() ;
  109. //
  110. //}
  111.     TQ3Status theStatus ;
  112.         
  113.     if((**theDocument).fModel != nil ) {
  114.         HLock((Handle)theDocument );
  115.         Q3View_StartRendering((**theDocument).fView) ;
  116.         do {
  117.             theStatus = SubmitScene( theDocument ) ;
  118.         } while (Q3View_EndRendering((**theDocument).fView) == kQ3ViewStatusRetraverse );
  119.     
  120.         HUnlock((Handle)theDocument );
  121.     }    
  122.     return theStatus ;    
  123.  
  124.     
  125. }
  126.  
  127.  
  128. //----------------------------------------------------------------------------------
  129.  
  130. //-------------------------------------------------------------------------------------------
  131. //
  132.  
  133. short HiWrd(long aLong)
  134. {
  135.     return    (((aLong) >> 16) & 0xFFFF) ;
  136. }
  137.  
  138. //-------------------------------------------------------------------------------------------
  139. //
  140.  
  141. short LoWrd(long aLong)
  142. {
  143.     return    ((aLong) & 0xFFFF) ;
  144.  
  145. }
  146. //-------------------------------------------------------------------------------------------
  147. //    IsAppWindow
  148. //
  149. //    Check to see if a window belongs to the application. If the window pointer
  150. //    passed was NIL, then it could not be an application window. WindowKinds
  151. //    that are negative belong to the system and windowKinds less than userKind
  152. //    are reserved by Apple except for windowKinds equal to dialogKind, which
  153. //    mean it is a dialog.
  154.  
  155. Boolean IsAppWindow(WindowPtr window)
  156. {
  157.     short        windowKind;
  158.  
  159.     if ( window == nil )
  160.         return false;
  161.     else {
  162.         windowKind = ((WindowPeek) window)->windowKind;
  163.         return ((windowKind >= userKind) || (windowKind == dialogKind));
  164.     }
  165. }
  166.  
  167.  
  168. //-------------------------------------------------------------------------------------------
  169. //    IsDAWindow
  170. //
  171. //    Check to see if a window belongs to a desk accessory. It belongs to a DA
  172. //    if the windowKind field of the window record is negative.
  173.  
  174. Boolean IsDAWindow(WindowPtr window)
  175. {
  176.     if ( window == nil )
  177.         return false;
  178.     else
  179.         return ( ((WindowPeek) window)->windowKind < 0 );
  180. }
  181.  
  182.  
  183. //-------------------------------------------------------------------------------------------
  184. //    IsDialogWindow
  185. //
  186. //    Check to see if a window is a dialog window. We can determine this be
  187. //    checking to see if the windowKind field is equal to dialogKind.
  188.  
  189. Boolean IsDialogWindow(WindowPtr window)
  190. {
  191.     if ( window == nil )
  192.         return false;
  193.     else
  194.         return ( ((WindowPeek) window)->windowKind == dialogKind );
  195. }
  196.  
  197.  
  198. //-------------------------------------------------------------------------------------------
  199. //
  200. void AdjustMenus()
  201. {
  202.     WindowPtr    window;
  203.     MenuHandle    menu;
  204.  
  205.     window = FrontWindow();
  206.  
  207.     menu = GetMHandle(mFile);
  208.     
  209.     if ( window != nil )
  210.         EnableItem(menu, iClose);
  211.     else
  212.         DisableItem(menu, iClose);
  213.  
  214.     menu = GetMHandle(mEdit);
  215.     if ( IsDAWindow(window) ) {
  216.         EnableItem(menu, iUndo);
  217.         EnableItem(menu, iCut);
  218.         EnableItem(menu, iCopy);
  219.         EnableItem(menu, iClear);
  220.         EnableItem(menu, iPaste);
  221.     } else {                        
  222.         DisableItem(menu, iUndo);
  223.         DisableItem(menu, iCut);
  224.         DisableItem(menu, iCopy);
  225.         DisableItem(menu, iClear);
  226.         DisableItem(menu, iPaste);
  227.     }
  228. }
  229.  
  230. //-------------------------------------------------------------------------------------------
  231. //
  232. void InitToolbox()
  233. {
  234.     Handle        menuBar;
  235.     EventRecord event;
  236.     short        count;
  237.  
  238.     // initialize application globals
  239.     gQuitFlag = false;
  240.     gWatchCursor = GetCursor( watchCursor ) ;
  241.     SetCursor( *gWatchCursor ) ;
  242.     
  243.     MaxApplZone() ;
  244.     MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  245.     
  246.     // Init the managers
  247.     InitGraf( &qd.thePort );
  248.     InitFonts();
  249.     InitWindows();
  250.     InitMenus();
  251.     TEInit();
  252.     InitDialogs(nil);
  253.  
  254.  
  255.     FlushEvents( everyEvent, 0 ) ;
  256.         
  257.     //    This next bit of code waits until the Finder brings our application
  258.     //    to the front. This gives us a better effect if we open a window at
  259.     //    startup.
  260.  
  261.     for (count = 1; count <= 3; ++count)
  262.         EventAvail(everyEvent, &event);
  263.  
  264.     menuBar = GetNewMBar( 128 );
  265.     if ( menuBar == nil )
  266.          ExitToShell() ;
  267.          
  268.     SetMenuBar( menuBar );
  269.     DisposHandle( menuBar );
  270.     AddResMenu( GetMHandle(mApple), 'DRVR' );    // Add DA names to Apple menu
  271.     AdjustMenus();
  272.     DrawMenuBar();
  273.     InitCursor();                                // reset the cursor
  274. }
  275.  
  276. //-------------------------------------------------------------------------------------------
  277. //    HandleMenuCommand
  278. //
  279. //    This is called when an item is chosen from the menu bar (after calling
  280. //    MenuSelect or MenuKey). It performs the right operation for each command.
  281. //    It is good to have both the result of MenuSelect and MenuKey go to one
  282. //    routine like this to keep everything organized.
  283.  
  284. void HandleMenuCommand(long    menuResult)
  285. {
  286.     short        menuID;                // Resource ID# of the selected menu
  287.     short        menuItem;            // Item number of the selected menu
  288.     Str255        daName;
  289.     DialogPtr    theDialog ; 
  290.     short        itemHit ;
  291.     WindowPtr    theWindow ;
  292.  
  293.     menuID = HiWrd(menuResult);
  294.     menuItem = LoWrd(menuResult);
  295.     switch ( menuID ) {
  296.         case mApple:
  297.             switch ( menuItem ) {
  298.                 ModalFilterUPP         theProc ;
  299.  
  300.                 case iAbout:
  301.                 
  302.                     theDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
  303.                     
  304.                     // these two lil' snappers are system 7 only
  305.                     // so if you use them, check before!!
  306.                     GetStdFilterProc( &theProc ) ;
  307.                     SetDialogDefaultItem(theDialog, ok) ;
  308.                     
  309.                     
  310.                     do {
  311.                         ModalDialog ( theProc, &itemHit );
  312.                     } while( itemHit != ok ) ;
  313.                     DisposDialog ( theDialog );
  314.                     break;
  315.  
  316.                 default:            // All non-About items in this menu are DAs
  317.                     GetItem(GetMHandle(mApple), menuItem, daName);
  318.                     (void) OpenDeskAcc(daName);
  319.                     break;
  320.             }
  321.             break;
  322.         case mFile:
  323.             switch ( menuItem ) {
  324.                 case iNew:
  325.                     HandleNewCommand() ;
  326.                     break;
  327.                 case iOpen:
  328.                     HandleOpenCommand() ;
  329.                     break;
  330.                 case iClose:
  331.                     HandleCloseWindow( FrontWindow() ) ;
  332.                     break;
  333.                 case iQuit:
  334.                     gQuitFlag = true;
  335.                     break;
  336.             }
  337.             break;
  338.         case mEdit:
  339.             switch (menuItem) {
  340.                 // Call SystemEdit for DA editing & MultiFinder 
  341.                 // since we don’t do any Editing yet
  342.                 case iUndo:
  343.                 case iCut:
  344.                 case iCopy:
  345.                 case iPaste:
  346.                 case iClear:
  347.                     (void) SystemEdit(menuItem-1);
  348.                     break;
  349.             }
  350.             break;
  351.         case mGeometry:
  352.             theWindow = FrontWindow() ;
  353.             if( theWindow ) {
  354.                 DocumentHdl theDocument = (DocumentHdl) GetWRefCon( theWindow ) ;
  355.                 if( theDocument != nil ) {
  356.                     TQ3Point3D        myOrigin = { 0, 0, 0 } ;
  357.                 
  358.                     HLock( (Handle)theDocument ) ;
  359.                     
  360.                     if((**theDocument).fModel != nil) {
  361.                         Q3Object_Dispose((**theDocument).fModel) ;                
  362.                         (**theDocument).fModel = nil ;
  363.                     }
  364.                     
  365.                     (**theDocument).fModel = BuildGeometry(menuItem) ;
  366.                     
  367.                     (**theDocument).fGroupScale = 1;                
  368.                     (**theDocument).fGroupCenter = myOrigin ;            
  369.                     AdjustCamera(    theDocument,
  370.                                     (theWindow->portRect.right - theWindow->portRect.left),
  371.                                     (theWindow->portRect.bottom - theWindow->portRect.top) ) ;
  372.                                     
  373.                     HUnlock((Handle)theDocument);
  374.                 }
  375.             }
  376.             SetPort( (GrafPtr)theWindow ) ;
  377.             InvalRect( & theWindow->portRect ) ;
  378.             
  379.             break ;
  380.     }
  381.     HiliteMenu(0);        // Unhighlight what MenuSelect or MenuKey hilited
  382. }
  383.  
  384. //-------------------------------------------------------------------------------------------
  385. //
  386. void MainEventLoop()
  387. {
  388.     EventRecord     event;
  389.     WindowPtr       window;
  390.     short           thePart;
  391.     Rect            screenRect, updateRect;
  392.     Point            aPoint = {100, 100};
  393.     CGrafPtr        savedPort ;
  394.     
  395.     DocumentHdl        theDocument ;
  396.     
  397.  
  398.     while( !gQuitFlag )
  399.     {
  400.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  401.         {
  402.  
  403.             switch (event.what) {
  404.                 case mouseDown:
  405.                 
  406.                     thePart = FindWindow( event.where, &window );
  407.                     
  408.                     switch( thePart ) {
  409.                         case inMenuBar: 
  410.                             AdjustMenus();
  411.                             HandleMenuCommand(MenuSelect(event.where));
  412.                             break;
  413.                         
  414.                         case inDrag:
  415.                     
  416.                             screenRect = (**GetGrayRgn()).rgnBBox;
  417.                             DragWindow( window, event.where, &screenRect );
  418.                             break ;
  419.                     
  420.                         case inContent:
  421.                     
  422.                             if (window != FrontWindow())
  423.                                 SelectWindow( window );
  424.                             break ;
  425.                     
  426.                         case inGoAway:
  427.                             if (TrackGoAway( window, event.where )) {
  428.                                 HandleCloseWindow( window ) ;
  429.                             }
  430.                             break ;
  431.                             
  432.                         default:
  433.                             break ;
  434.                     }
  435.                     break ;
  436.                             
  437.                         
  438.                 case updateEvt:
  439.                 
  440.                     window = (WindowPtr)event.message;
  441.                     theDocument = (DocumentHdl) GetWRefCon( window ) ;
  442.                     
  443.                     if( theDocument != nil ) {
  444.                         updateRect = (**(window->visRgn)).rgnBBox;
  445.                         
  446.                         GetPort( (GrafPtr *)&savedPort) ;
  447.                         SetPort( window ) ;
  448.                         BeginUpdate( window );
  449.                         DocumentDraw3DData( theDocument ) ;
  450.                         EndUpdate( window );
  451.                         SetPort( (GrafPtr)savedPort ) ;
  452.                     }
  453.  
  454.                     break ;
  455.                     
  456.                 case keyDown:
  457.                 case autoKey:
  458.                     HandleKeyPress(&event);
  459.                     break;
  460.                     
  461.                 case diskEvt:
  462.                     if ( HiWrd(event.message) != noErr ) 
  463.                         (void) DIBadMount(aPoint, event.message);
  464.                     break;
  465.                     
  466.                 case osEvt:
  467.                 case activateEvt:
  468.                     break;
  469.  
  470.  
  471.             }
  472.         }
  473.         else {
  474.             WindowPtr    theWindow ;
  475.             
  476.             theWindow = FrontWindow() ;
  477.             if( theWindow ) {
  478.                 theDocument = (DocumentHdl) GetWRefCon( window ) ;
  479.                 if( theDocument != nil ) {
  480.  
  481.                     // we received a null event, rotate the object
  482.                     TQ3Matrix4x4    tmp;
  483.                     Rect        theRect = ((GrafPtr)theWindow)->portRect ;
  484.                     
  485.                     SetPort((GrafPtr)theWindow) ;
  486.                     Q3Matrix4x4_SetRotate_XYZ(&tmp, 0.1, 0.12, 0.08);
  487.                     HLock( (Handle)theDocument ) ;
  488.                     Q3Matrix4x4_Multiply(&(**theDocument).fRotation, &tmp, &(**theDocument).fRotation);
  489.                     HUnlock( (Handle)theDocument ) ;
  490.         
  491.                     InvalRect( &theRect ) ;
  492.                 }
  493.             }
  494.         }
  495.     }
  496. }
  497.  
  498.  
  499. //-------------------------------------------------------------------------------------------
  500. //
  501. void HandleKeyPress(EventRecord *event)
  502. {
  503.     char    key;
  504.  
  505.     key = event->message & charCodeMask;
  506.     if ( event->modifiers & cmdKey ) {        /* Command key down? */
  507.         AdjustMenus();                        /* Enable/disable/check menu items properly */
  508.         HandleMenuCommand(MenuKey(key));
  509.     } else {
  510.         /* DoKeyPress(event) */;
  511.     }
  512. }
  513.  
  514. //-------------------------------------------------------------------------------------------
  515. //
  516.  
  517.  
  518.  
  519.